<?php

namespace IDock\Insurance\JTIC\Request;

use IDock\Insurance\JTIC\JTICEnums;
use IDock\Insurance\JTIC\Response\UploadFileResponse;
use IDock\Visitor\UploadFileVisitor;

class UploadFileRequest extends AllJTICRequest
{
    protected $code = 'V106';

    protected $response = UploadFileResponse::class;

    protected $urlTip = '影像上传';

    protected $inLog = false;

    /*** @var  UploadFileVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        return [
            'BasePart' => [
                'QtnId' => $this->visitor->argc['QtnId'] ?? '',
            ],
            'AppFileList' => [
                'FileItem' => $this->imageList()
            ]
        ];
    }

    private function imageList(): array
    {
        $list = [];
        foreach ($this->visitor->getFileList() as $file) {
            if (!$file['url'] ?? '') {
                continue;
            }
            $fileName = JTICEnums::$uploadFileTypeName[(JTICEnums::$uploadFileType[$file['type']] ?? '99')] ?? '';

            $fileName = $fileName . '-' . rand(10000, 99999);

            $imageBase64 = base64_encode(file_get_contents($file['url']));
            $extArr = explode('.', basename($file['url']));
            $ext = end($extArr);
            $list[] = [
                'FileType' => JTICEnums::$uploadFileType[$file['type']] ?? '99',
                'FileName' => $fileName,
                'FileSuffix' => $ext,
                'FileBase64' => $imageBase64,
            ];
        }

        return $list;
    }
}
